

#include "multirectangle.h"
#include <stdio.h>

//
//////////////////////////////////////////////////////////////////////////
multiRectangle::multiRectangle(void)
{
  rectangles.clear();
  m_Area = 0;
  m_Ix = 0;
  m_Iy = 0;
  m_Cx = m_Cy = 0; 
}

//
//////////////////////////////////////////////////////////////////////////
multiRectangle::~multiRectangle(void)
{
  rectangles.clear();
}

//
//////////////////////////////////////////////////////////////////////////
void multiRectangle::computeProperties()
{
  int i;
  int n = (int) rectangles.size();
  double prod_x = 0, prod_y = 0;

  m_Area = 0;
  for (i = 0; i < n; ++i)
  {
    double area_tst = rectangles[i].area();
    m_Area += rectangles[i].area();
    prod_x += rectangles[i].area()*rectangles[i].center_x();
    prod_y += rectangles[i].area()*rectangles[i].center_y();
  }

  // center
  m_Cx = prod_x / m_Area;
  m_Cy = prod_y / m_Area;

  // set new coordinates
  double xmn, ymn, xmx, ymx;
  for (i = 0; i < n; ++i)
  {
    xmn = rectangles[i].getXmin() - m_Cx;
    xmx = rectangles[i].getXmax() - m_Cx;
    ymn = rectangles[i].getYmin() - m_Cy;
    ymx = rectangles[i].getYmax() - m_Cy;
    rectangles[i].setCoords(xmn, ymn, xmx, ymx);
  }

  // inercia moment
  m_Ix = m_Iy = 0;
  for (i = 0; i < n; ++i)
  {
    m_Ix += rectangles[i].Ix_cg() + rectangles[i].center_y()*rectangles[i].center_y()*rectangles[i].area();
    m_Iy += rectangles[i].Iy_cg() + rectangles[i].center_x()*rectangles[i].center_x()*rectangles[i].area();
  }

}

//
//////////////////////////////////////////////////////////////////////////
double multiRectangle::getMaxX()
{
  int i;
  int n = (int) rectangles.size();
  if (n == 0)
    return 0.0;

  double x = rectangles[0].getXmax();
  for (i = 1; i < n; ++i)
  {
    if (rectangles[i].getXmax() > x)
      x = rectangles[i].getXmax();
  }

  return x;
}

//
//////////////////////////////////////////////////////////////////////////
double multiRectangle::getMaxY()
{
  int i;
  int n = (int) rectangles.size();
  if (n == 0)
    return 0.0;

  double y = rectangles[0].getYmax();
  for (i = 1; i < n; ++i)
  {
    if (rectangles[i].getYmax() > y)
      y = rectangles[i].getYmax();
  }

  return y;
}

//
//////////////////////////////////////////////////////////////////////////
double multiRectangle::getCrossMinimunWidth( double _ref )
{
  int i;
  int n = (int) rectangles.size();
  bool flag = true;
  double bmin = 0.0;

  if (n == 0)
    return 0.0;

  for (i = 0; i < n; ++i)
  {
    double xmin, ymin, xmax, ymax;
    xmin = rectangles[i].getXmin();
    xmax = rectangles[i].getXmax();
    ymin = rectangles[i].getYmin();
    ymax = rectangles[i].getYmax();

    if (ymax >= _ref && ymin <= _ref)
    {
      bmin += (rectangles[i].getXmax() - rectangles[i].getXmin());

      //if ((xmax - xmin) < bmin || flag)
      //{
      //  bmin = rectangles[i].getXmax() - rectangles[i].getXmin();
      //  flag = false;
      //}
    }
  }

  return bmin;
}

//
//////////////////////////////////////////////////////////////////////////
double multiRectangle::getStaticMoment( double _ref )
{
  int i;
  int n = (int) rectangles.size();
  bool flag = true;
  double static_m = 0.0;

  if (n == 0)
    return 0.0;

  for (i = 0; i < n; ++i)
  {
    if (_ref < rectangles[i].getYmax()  && _ref >= rectangles[i].getYmin())
    {
      double b = rectangles[i].getXmax() - rectangles[i].getXmin();
      double y1 = (rectangles[i].getYmax() - _ref);
      double area = y1 * b;
      static_m += area * y1 * 0.5;
    }
    else if (rectangles[i].getYmin() > _ref)
    {
      double area = rectangles[i].area();
      double dist = rectangles[i].center_y() - _ref;
      static_m += area * dist;
    }
  }

  return static_m;
}

//
//////////////////////////////////////////////////////////////////////////
int multiRectangle::read( char *fn )
{
  int n, i;
  double xmn, ymn, xmx, ymx;

  FILE *ptr = fopen (fn, "rt");
  if (ptr == NULL)
    return 0;

  fscanf (ptr, "%d", &n);

  for (i = 0; i < n; ++i)
  {
    rectangle rec;
    fscanf (ptr, "%lf %lf %lf %lf", &xmn, &ymn, &xmx, &ymx);
    rec.setCoords(xmn*0.001, ymn*0.001, xmx*0.001, ymx*0.001);
    rectangles.push_back(rec);
  }

  return 1;
}

//
//////////////////////////////////////////////////////////////////////////
void multiRectangle::drawMatlab( char *fn )
{
  int    i;
  double xmn, ymn, xmx, ymx;

  FILE *ptr = fopen (fn, "wt");
  if (ptr == NULL)
    return;

  int n = (int) rectangles.size();
  bool flag = true;
  double static_m = 0.0;

  if (n == 0)
    return;

  for (i = 0; i < n; ++i)
  {
    xmn = rectangles[i].getXmin();
    xmx = rectangles[i].getXmax();
    ymn = rectangles[i].getYmin();
    ymx = rectangles[i].getYmax();

    fprintf(ptr, "rectangle('Position',[%lf, %lf, %lf, %lf], 'FaceColor','r')\n", xmn, ymn, xmx-xmn, ymx-ymn);

  }

  fclose(ptr);
}
